package exercise;

import java.util.Stack;

public class TestSort {
    public static void insertSort(int[] arr) {
        for (int i = 1; i < arr.length; i++) {
            int tmp = arr[i];
            int j = i - 1;
            for (; j >= 0; j--) {
                if (arr[j] > tmp) {
                    arr[j + 1] = arr[j];
                } else {
                    break;
                }
            }
            arr[j + 1] = tmp;
        }
    }

    public static void shellSort(int[] arr) {
        int gap = arr.length;
        while (gap > 1) {
            gap = gap / 2;
            shell(arr, gap);
        }
    }

    private static void shell(int[] arr, int gap) {
        for (int i = gap; i < arr.length; i++) {
            int tmp = arr[i];
            int j = i - gap;
            for (; j >= 0; j -= gap) {
                if (arr[j] > tmp) {
                    arr[j + gap] = arr[j];
                } else {
                    break;
                }
            }
            arr[j + gap] = tmp;
        }
    }

    public static void selectSort(int[] arr) {
        for (int i = 0; i < arr.length; i++) {
            int minIndex = i;
            for (int j = i + 1; j < arr.length; j++) {
                if (arr[j] < arr[minIndex]) {
                    minIndex = j;
                }
            }
            swap(arr, i, minIndex);
        }
    }

    private static void swap(int[] arr, int i, int j) {
        int tmp = arr[i];
        arr[i] = arr[j];
        arr[j] = tmp;
    }

    public static void selectSort2(int[] arr) {
        int left = 0, right = arr.length - 1;
        while (left < right) {
            int minIndex = left;
            int maxIndex = right;
            for (int i = left; i <= right; i++) {
                if (arr[i] < arr[minIndex]) {
                    minIndex = i;
                }
                if (arr[i] > arr[maxIndex]) {
                    maxIndex = i;
                }
            }
            swap(arr, left, minIndex);
            if (maxIndex == left) {
                maxIndex = minIndex;
            }
            swap(arr, maxIndex, right);
            left++;
            right--;
        }
    }

    //堆排序
    public static void heapSort(int[] arr) {
        createBigHeap(arr);
        int end = arr.length - 1;
        while (end > 0) {
            swap(arr, 0, end);
            siftDown(arr, 0, end);
            end--;
        }
    }

    private static void createBigHeap(int[] arr) {
        int parent = (arr.length - 1 - 1) / 2;
        for (; parent >= 0; parent--) {
            siftDown(arr, parent, arr.length);
        }
    }

    private static void siftDown(int[] arr, int parent, int end) {
        int child = parent * 2 + 1;
        while (child < end) {
            if (child + 1 < end && arr[child + 1] > arr[child]) {
                child++;
            }
            if (arr[child] > arr[parent]) {
                swap(arr, child, parent);
                parent = child;
                child = 2 * parent + 1;
            } else {
                break;
            }
        }
    }

    public static void bubbleSort(int[] arr) {
        for (int i = 0; i < arr.length; i++) {
            boolean flg = true;
            for (int j = 0; j < arr.length - 1 - i; j++) {
                if (arr[j] > arr[j + 1]) {
                    swap(arr, j, j + 1);
                    flg = false;
                }
            }
            if (flg) {
                break;
            }
        }
    }

    //快速排序
    public static void quickSort(int[] arr) {
        quick(arr,0,arr.length-1);
    }
    private static void quick(int[] arr,int start,int end) {
        if(start >= end) {
            return;
        }

        //当区间长度小于10时,采用插入排序并返回
        if(end-start+1<10) {
            insertSortRange(arr,start,end);
            return;
        }

//        System.out.println("start:"+start+",end:"+end);

        //三数取中法,1 2 3 4 5 -> 3 2 1 4 5
        int index=midThreeNum(arr,start,end);
        swap(arr,start,index);

        int pivot=partition(arr,start,end);
        quick(arr,start,pivot-1);
        quick(arr,pivot+1,end);
    }
    private static int partitionHoare(int[] arr,int left,int right) {
        int tmp=arr[left];//基准值
        int i=left;
        while(left<right) {
            while(left<right && arr[right]>=tmp) {
                right--;
            }
            while(left<right && arr[left]<=tmp) {
                left++;
            }
            swap(arr, right, left);
        }
        //left和right相遇了
        swap(arr,i,left);
        return left;
    }

    private static int partition(int[] arr,int left,int right) {
        int tmp=arr[left];
        while(left<right) {
            while(left<right && arr[right]>=tmp) {
                right--;
            }
            arr[left]=arr[right];//将right找到的值填到left处的坑里
            while(left<right && arr[left]<=tmp) {
                left++;
            }
            arr[right]=arr[left];
        }
        //left和right相遇了
        arr[left]=tmp;
        return left;
    }

    //三数取中法
    private static int midThreeNum(int[] arr,int start,int end) {
        int mid=(start+end)/2;
        if(arr[start]>arr[end]) {
            if(arr[mid]<arr[end]) {
                return end;
            }else if(arr[start]>arr[mid]) {
                return mid;
            }else {
                return start;
            }
        }else {
            if(arr[mid]<arr[start]) {
                return start;
            }else if(arr[end]>arr[mid]) {
                return mid;
            }else {
                return end;
            }
        }
    }

    public static void insertSortRange(int[] arr,int left,int right) {
        for(int i=left+1;i<=right;i++) {
            int tmp=arr[i];//tmp中存储是要插入的对象
            int j=i-1;
            for(;j>=left;j--) {   //将tmp前面的数一个个与tmp做比较
                if(arr[j]>tmp) {    //若是tmp更小,那么将该值后移
                    arr[j+1]=arr[j];
                }else {             //后移直到tmp更大时,跳出循环
                    break;
                }
            }
            arr[j+1]=tmp;
        }
    }

    public static void quickSortNot(int[] arr) {
        Stack<Integer> stack=new Stack<>();
        int left=0;
        int right=arr.length-1;
        int pivot=partition(arr,left,right);
        if(pivot>left+1) {
            stack.push(left);
            stack.push(pivot-1);
        }
        if(pivot<right-1) {
            stack.push(pivot+1);
            stack.push(right);
        }
        while(!stack.isEmpty()) {
            right=stack.pop();
            left=stack.pop();
            pivot=partition(arr,left,right);
            if(pivot>left+1) {
                stack.push(left);
                stack.push(pivot-1);
            }
            if(pivot<right-1) {
                stack.push(pivot+1);
                stack.push(right);
            }
        }
    }

    //归并排序
    public static void mergeSort(int[] arr) {
        mergeSortFunc(arr,0,arr.length-1);
    }
    private static void mergeSortFunc(int[] arr,int left,int right) {
        if(left>=right)return;
        int mid=(left+right)/2;
        mergeSortFunc(arr,left,mid);
        mergeSortFunc(arr,mid+1,right);
        merge(arr,left,mid,right);
    }
    private static void merge(int[] arr,int left,int mid,int right) {
        int s1=left;
        int e1=mid;
        int s2=mid+1;
        int e2=right;
        int[] tmp=new int[right-left+1];
        int k=0;
        while(s1<=e1 && s2<=e2) {
            if(arr[s1]<=arr[s2]) {
                tmp[k++]=arr[s1++];
            }else {
                tmp[k++]=arr[s2++];
            }
        }
        while(s1<=e1) {
            tmp[k++]=arr[s1++];
        }
        while(s2<=e2) {
            tmp[k++]=arr[s2++];
        }
        for(int i=0;i<k;i++) {
            arr[i+left]=tmp[i];
        }
    }

    //归并排序的非递归实现
    public static void mergeSortNot(int[] arr) {
        int gap=1;
        while(gap<arr.length) {
            for (int i = 0; i < arr.length; i += 2 * gap) {
                int left = i;
                int mid = i + gap - 1;
                if(mid>=arr.length) {
                    mid=arr.length-1;
                }
                int right = mid + gap;
                if(right>=arr.length) {
                    right=arr.length-1;
                }
                merge(arr, left, mid, right);
            }
            gap*=2;
        }
    }

}




